package com.example.ziliao.utils

import android.graphics.Color
import android.view.View
import android.widget.Button
import android.widget.TextView

object ViewHelper {
    fun resetButtonState(
        btnOptionA: Button,
        btnOptionB: Button,
        btnOptionC: Button,
        btnOptionD: Button,
        btnOptionE: Button? = null
    ) {
        // 重置所有按钮的状态
        btnOptionA.setBackgroundColor(0xFFE0E0E0.toInt())
        btnOptionB.setBackgroundColor(0xFFE0E0E0.toInt())
        btnOptionC.setBackgroundColor(0xFFE0E0E0.toInt())
        btnOptionD.setBackgroundColor(0xFFE0E0E0.toInt())
        btnOptionE?.setBackgroundColor(0xFFE0E0E0.toInt())
        
        btnOptionA.isEnabled = true
        btnOptionB.isEnabled = true
        btnOptionC.isEnabled = true
        btnOptionD.isEnabled = true
        btnOptionE?.isEnabled = true
        
        // 恢复选项E的可见性（如果存在）
        btnOptionE?.visibility = View.VISIBLE
    }

    fun handleOptionClick(
        selectedIndex: Int,
        correctIndex: Int,
        btnOptionA: Button,
        btnOptionB: Button,
        btnOptionC: Button,
        btnOptionD: Button,
        btnOptionE: Button? = null
    ) {
        val buttons = listOfNotNull(btnOptionA, btnOptionB, btnOptionC, btnOptionD, btnOptionE)
        
        if (selectedIndex == correctIndex) {
            buttons[selectedIndex].setBackgroundColor(Color.GREEN)
        } else {
            buttons[selectedIndex].setBackgroundColor(Color.RED)
            buttons[correctIndex].setBackgroundColor(Color.GREEN)
        }
        
        buttons.forEach { it.isEnabled = false }
    }

    fun resetResultText(tvExactResult: TextView) {
        tvExactResult.text = ""
        tvExactResult.visibility = View.GONE
    }

    fun showResultText(tvExactResult: TextView, resultPrefix: String, value: Double) {
        tvExactResult.text = "$resultPrefix：${String.format("%.2f", value)}"
        tvExactResult.visibility = View.VISIBLE
    }
} 